﻿using MediatR;
using T.S.BaseData.Api.AppClinet.Commend.Goods;
using T.S.BaseData.Domain.Entity;
using T.S.BaseData.ErrorCode;
using T.S.BaseData.Infrastructure.Base;

namespace T.S.BaseData.Api.AppClinet.CommendHand.Goods
{
    public class BasicinfoDelCommandHandle : IRequestHandler<BasicinfoDelCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<BasicInfo> basicRepo;
        private readonly ILogger<BasicinfoDelCommandHandle> logger;

        public BasicinfoDelCommandHandle(IBaseRepository<BasicInfo> basicRepo,
            ILogger<BasicinfoDelCommandHandle> logger)
        {
            this.basicRepo = basicRepo;
            this.logger = logger;
        }

        public Task<ApiResult<int>> Handle(BasicinfoDelCommand request, CancellationToken cancellationToken)
        {

            ApiResult<int> result = new ApiResult<int>();

            var model = basicRepo.GetModel(request.Id);

            model.IsDelete = true;

            var r = basicRepo.Update(model);
            if (r>0)
            {
                result.Code = ApiEnums.Success;
                result.Data = r;
                result.Meg = "删除成功";

                logger.LogInformation("删除成功");
            }
            else
            {
                result.Code = ApiEnums.Error;
                result.Data = r;
                result.Meg = "删除失败";

                logger.LogInformation("删除失败");
            }

            return Task.FromResult(result);

        }
    }
}
